<template>
  <div class="dialog-container">
    <el-dialog
      :title="dialogType==='add'?'添加设备类型':dialogType === 'detail'?'设备类型详情':'修改设备类型'"
      :visible="dialogVisible"
      @open="initData"
      @close="closeDialog"
    >
      <el-form
        ref="form"
        :model="dataForm"
        class="dialog-form"
        :rules="rules"
        label-width="110px"
        size="mini"
        :disabled="dialogType === 'detail'"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备类型名称" prop="name">
              <el-input v-model="dataForm.name" placeholder="请输入设备类型名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备品牌" prop="eqmBrand">
              <el-input v-model="dataForm.eqmBrand" placeholder="请输入设备品牌" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备类型简称" prop="shortName">
              <el-input v-model="dataForm.shortName" placeholder="请输入设备类型简称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备型号" prop="eqmModel">
              <el-input v-model="dataForm.eqmModel" placeholder="请输入设备型号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="监测污染物" prop="contaminants">
              <el-input v-model="dataForm.contaminants" placeholder="请输入监测污染物" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="监测类型" prop="monitorType">
              <el-select v-model="dataForm.monitorType" class="w-100" placeholder="请输入监测类型">
                <el-option
                  v-for="(item,index) in monitorTypes"
                  :key="index+'monitor_type'"
                  :value="item.code"
                  :label="item.codeName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仪器最小量程值" prop="minValue">
              <el-input v-model="dataForm.minValue" placeholder="请输入仪器最小量程值" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仪器最大量程值" prop="maxValue">
              <el-input v-model="dataForm.maxValue" placeholder="请输入仪器最大量程值" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="产品证书编号" prop="certificateNumber">
              <el-input v-model="dataForm.certificateNumber" placeholder="请输入产品证书编号" />
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="生产商" prop="manufacturer">
              <el-input v-model="dataForm.manufacturer" placeholder="请输入生产商" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产商联系人" prop="manufacturerContact">
              <el-input v-model="dataForm.manufacturerContact" placeholder="请输入生产商联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="contactNumber">
              <el-input v-model="dataForm.contactNumber" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <!--          <el-col :span="12">-->
          <!--            <el-form-item label="联系邮箱" prop="caseCode">-->
          <!--              <el-input v-model="dataForm.remark" placeholder="请输入备注" />-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="22">
            <el-form-item label="测量原理" prop="measuringPrinciple">
              <el-input v-model="dataForm.measuringPrinciple" placeholder="请输入测量原理" />
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="dataForm.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="4">
            <el-form-item label="产品证书图片" prop="">
              <UploadImage
                :url="dataForm.certificateImageUrl"
                @setUrl="setUrl"
              />
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <span v-if="dialogType !== 'detail'" slot="footer" class="dialog-footer">
        <el-button :loading="saveLoading" class="sure-btn" size="mini" type="primary" @click="handleSave">保存</el-button>
        <el-button size="mini" class="cancel-btn" @click="closeDialog">取消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { getDict } from '@/utils/dict'
import { add, edit } from '@/api/info/equipmentType'
// import UploadImage from '@/components/UploadImage/index'

export default {
  name: 'AddEquipmentTypeDialog',
  // components: { UploadImage },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    dialogType: {
      type: String,
      default: 'detail'
    },
    obj: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      saveLoading: false,
      dataForm: {
        id: null,
        name: null,
        shortName: null,
        monitorType: null,
        shortNamePym: null,
        eqmBrand: null,
        eqmModel: null,
        contaminants: null,
        certificateNumber: null,
        maxValue: null,
        minValue: null,
        manufacturer: null,
        manufacturerContact: null,
        contactNumber: null,
        measuringPrinciple: null,
        remark: null,
        certificateImageUrl: ''
      },
      monitorTypes: [],
      equipmentTypes: [],
      dialogImageUrl: '',
      rules: {
        name: [{ required: true, message: '请输入设备类型名称', trigger: 'blur' }],
        shortName: [{ required: true, message: '请输入设备类型简称', trigger: 'blur' }],
        eqmBrand: [{ required: true, message: '请输入设备品牌', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.monitorTypes = getDict('e052')
  },
  methods: {
    initData() {
      this.saveLoading = false
      if (this.obj) {
        this.dataForm = JSON.parse(JSON.stringify(this.obj))
      }
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    handleSave() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.saveLoading = true
          if (this.dataForm.id) {
            edit(this.dataForm).then(() => {
              this.$message({
                type: 'success',
                message: '修改成功'
              })
              this.$emit('flushPage')
              this.$emit('closeDialog')
              this.clearData()
              this.saveLoading = false
            }).catch(() => {
              this.saveLoading = false
            })
          } else {
            add(this.dataForm).then(() => {
              this.$message.success('添加成功')
              this.$emit('flushPage')
              this.$emit('closeDialog')
              this.clearData()
              this.saveLoading = false
            }).catch(() => {
              this.saveLoading = false
            })
          }
        }
      })
    },
    closeDialog() {
      this.$emit('closeDialog')
      this.clearData()
      this.$refs['form'].clearValidate()
    },
    clearData() {
      this.dataForm = {
        id: null,
        shortName: null,
        monitorType: null,
        shortNamePym: null,
        eqmBrand: null,
        eqmModel: null,
        contaminants: null,
        certificateNumber: null,
        maxValue: null,
        minValue: null,
        manufacturer: null,
        manufacturerContact: null,
        contactNumber: null,
        measuringPrinciple: null,
        remark: null,
        certificateImageUrl: ''
      }
    },
    setUrl(url) {
      this.dataForm.certificateImageUrl = url
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
